package com.feige.utils.exception;


import com.feige.utils.string.StrFormatter;
import com.feige.utils.string.StringUtils;

/**
 * 基础异常
 *
 * @author feige
 */
public class BaseException extends RuntimeException {
    private static final long serialVersionUID = 1L;
    /**
     * 错误码
     */
    private String code;

    /**
     * 错误消息
     */
    private String msg;

    /**
     * 错误码对应的参数
     */
    private Object[] args;


    public BaseException(String code, String msg, Object... args) {
        this.code = code;
        this.msg = msg;
        this.args = args;
    }

    public BaseException(String message, String code, String msg, Object... args) {
        super(message);
        this.code = code;
        this.msg = msg;
        this.args = args;
    }

    public BaseException(String message, Throwable cause, String code, String msg, Object... args) {
        super(message, cause);
        this.code = code;
        this.msg = msg;
        this.args = args;
    }

    public BaseException(Throwable cause, String code, String msg, Object... args) {
        super(cause);
        this.code = code;
        this.msg = msg;
        this.args = args;
    }

    public BaseException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace, String code, String msg, Object[] args) {
        super(message, cause, enableSuppression, writableStackTrace);
        this.code = code;
        this.msg = msg;
        this.args = args;
    }

    public String getMsg() {
        return msg;
    }

    @Override
    public String getMessage(){
        String msg = "";
        if (args == null || args.length == 0){
            msg = getMsg();
        }else {
            msg = StrFormatter.format(getMsg(), getArgs());
        }
        if (StringUtils.isBlank(msg)){
            msg = super.getMessage();
        }
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public void setArgs(Object... args) {
        this.args = args;
    }

    public Object[] getArgs() {
        return args;
    }

}
